home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / chatsrvr / srvrdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  6.5 KB  |  315 lines

  1. // srvrdoc.cpp : implementation of the CServerDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1999 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "chatsrvr.h"
  15.  
  16. #include "srvrdoc.h"
  17. #include "srvrvw.h"
  18.  
  19. #include "msg.h"
  20. #include "dialogs.h"
  21.  
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CServerDoc
  30.  
  31. IMPLEMENT_DYNCREATE(CServerDoc, CDocument)
  32.  
  33. BEGIN_MESSAGE_MAP(CServerDoc, CDocument)
  34.     //{{AFX_MSG_MAP(CServerDoc)
  35.     //}}AFX_MSG_MAP
  36.     ON_UPDATE_COMMAND_UI(ID_MESSAGES, OnUpdateMessages)
  37.     ON_UPDATE_COMMAND_UI(ID_CONNECTIONS, OnUpdateConnections)
  38. END_MESSAGE_MAP()
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CServerDoc construction/destruction
  42.  
  43. CServerDoc::CServerDoc()
  44. {
  45.     m_pSocket = NULL;
  46. }
  47.  
  48. CServerDoc::~CServerDoc()
  49. {
  50.     delete m_pSocket;
  51. }                                                  
  52.  
  53. BOOL CServerDoc::OnNewDocument()                                         
  54. {
  55.     if (!CDocument::OnNewDocument())
  56.         return FALSE;
  57.  
  58.     CDiscussionDlg Dialog;
  59.     
  60.     if (Dialog.DoModal() == IDOK)
  61.     {
  62. #if defined(_WIN32_WCE)
  63.         // Establish a socket to listen for incoming connections.
  64.         m_pSocket = new CListeningSocket( this, CCeSocket::FOR_LISTENING );
  65. #else // _WIN32_WCE
  66.         m_pSocket = new CListeningSocket(this);
  67. #endif // _WIN32_WCE
  68.         if (m_pSocket->Create( Dialog.m_nPort+700 ))
  69.         {
  70.             if (m_pSocket->Listen())
  71.             {
  72.                 return TRUE;
  73.             }
  74.         }
  75.     }
  76.     return FALSE;
  77. }
  78.  
  79. void CServerDoc::DeleteContents() 
  80. {
  81.     delete m_pSocket;
  82.     m_pSocket = NULL;
  83.  
  84.     CString temp;
  85.     if (temp.LoadString(IDS_SERVERSHUTDOWN))
  86.         m_msgList.AddTail(temp);
  87.  
  88.     while (!m_connectionList.IsEmpty())
  89.     {
  90.         CClientSocket* pSocket = (CClientSocket*)m_connectionList.RemoveHead();
  91.         CMsg* pMsg = AssembleMsg(pSocket);
  92.         pMsg->m_bClose = TRUE;
  93.  
  94.         SendMsg(pSocket, pMsg);
  95.  
  96.         if (!pSocket->IsAborted())
  97.         {
  98.             pSocket->ShutDown();
  99.  
  100.             BYTE Buffer[50];
  101.  
  102.             while (pSocket->Receive(Buffer,50) > 0);
  103.  
  104.             delete pSocket;
  105.         }
  106.     }
  107.  
  108.     m_msgList.RemoveAll();
  109.         
  110.     if (!m_viewList.IsEmpty())
  111.         ((CEditView*)m_viewList.GetHead())->SetWindowText(_T(""));
  112.  
  113.     CDocument::DeleteContents();
  114. }
  115.  
  116. /////////////////////////////////////////////////////////////////////////////
  117. // CServerDoc serialization
  118.  
  119. void CServerDoc::Serialize(CArchive& ar)
  120. {
  121.     if (ar.IsStoring())
  122.     {
  123.         // CEditView contains an edit control which handles all serialization
  124.         ((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
  125.     }
  126. }
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // CServerDoc diagnostics
  130.  
  131. #ifdef _DEBUG
  132. void CServerDoc::AssertValid() const
  133. {
  134.     CDocument::AssertValid();
  135. }
  136.  
  137. void CServerDoc::Dump(CDumpContext& dc) const
  138. {
  139.     CDocument::Dump(dc);
  140. }
  141. #endif //_DEBUG
  142.  
  143. /////////////////////////////////////////////////////////////////////////////
  144. // CServerDoc Operations
  145.  
  146. void CServerDoc::UpdateClients()
  147. {
  148.     for (POSITION pos = m_connectionList.GetHeadPosition(); pos != NULL;)
  149.     {
  150.         CClientSocket* pSocket = (CClientSocket*)m_connectionList.GetNext(pos);
  151.         CMsg* pMsg = AssembleMsg(pSocket);
  152.  
  153.         if (pMsg != NULL)
  154.             SendMsg(pSocket, pMsg);
  155.     }
  156. }
  157.  
  158. void CServerDoc::ProcessPendingAccept() 
  159. {
  160.     CClientSocket* pSocket = new CClientSocket(this);
  161.  
  162.     // NOTE: This is where m_hSocket gets filled-in for the DataSocket.
  163.     if (m_pSocket->Accept(*pSocket))
  164.     {
  165.         pSocket->Init();
  166.         m_connectionList.AddTail(pSocket);
  167.     }
  168.     else
  169.         delete pSocket;
  170. }
  171.  
  172. void CServerDoc::ProcessPendingRead(CClientSocket* pSocket)
  173. {
  174.     do
  175.     {
  176.         CMsg* pMsg = ReadMsg(pSocket);
  177.  
  178.         if (pMsg->m_bClose)    
  179.         {
  180. #if defined(_WIN32_WCE)
  181.             // Send an FD_CLOSE message.
  182.             ::SendMessage( AfxGetCeSocketWindow(), WM_SOCKET_NOTIFY, 
  183.                           (WPARAM)pSocket->m_hSocket, (LPARAM)FD_CLOSE );
  184.  
  185. #endif // _WIN32_WCE
  186.             CloseSocket(pSocket);
  187.             break;
  188.         }
  189.     }
  190.     while (!pSocket->m_pArchiveIn->IsBufferEmpty());
  191.     
  192.     UpdateClients();
  193. }
  194.  
  195. CMsg* CServerDoc::AssembleMsg(CClientSocket* pSocket)
  196. {
  197.     static CMsg msg;
  198.  
  199.     msg.Init();
  200.  
  201.     if (pSocket->m_nMsgCount >= m_msgList.GetCount())
  202.         return NULL;
  203.  
  204.     for (POSITION pos1 = m_msgList.FindIndex(pSocket->m_nMsgCount); pos1 != NULL;)
  205.     {
  206.         CString temp = m_msgList.GetNext(pos1);
  207.         msg.m_msgList.AddTail(temp);
  208.     }
  209.     pSocket->m_nMsgCount = m_msgList.GetCount();
  210.     return &msg;
  211. }
  212.  
  213. CMsg* CServerDoc::ReadMsg(CClientSocket* pSocket)
  214. {
  215.     static CMsg msg;
  216.  
  217.     TRY
  218.     {
  219.         pSocket->ReceiveMsg(&msg);
  220.  
  221.         Message(msg.m_strText);
  222.  
  223.         m_msgList.AddTail(msg.m_strText);
  224.     }
  225.     CATCH(CFileException, e)
  226.     {
  227.         CString strTemp;
  228.         if (strTemp.LoadString(IDS_READERROR))
  229.             Message(strTemp);
  230.  
  231.         msg.m_bClose = TRUE;
  232.         pSocket->Abort();
  233.     }
  234.     END_CATCH
  235.  
  236.     return &msg;
  237. }
  238.  
  239. void CServerDoc::SendMsg(CClientSocket* pSocket, CMsg* pMsg)
  240. {
  241.     TRY
  242.     {
  243.         pSocket->SendMsg(pMsg);
  244.     }
  245.     CATCH(CFileException, e)
  246.     {
  247.         pSocket->Abort();
  248.  
  249.         CString strTemp;
  250.         if (strTemp.LoadString(IDS_SENDERROR))
  251.             Message(strTemp);
  252.     }
  253.     END_CATCH
  254. }
  255.  
  256.  
  257. void CServerDoc::CloseSocket(CClientSocket* pSocket)
  258. {
  259. #if !defined(_WIN32_WCE)
  260.     pSocket->Close();
  261. #endif // _WIN32_WCE
  262.  
  263.     POSITION pos,temp;
  264.     for (pos = m_connectionList.GetHeadPosition(); pos != NULL;)
  265.     {
  266.         temp = pos;
  267.         CClientSocket* pSock = (CClientSocket*)m_connectionList.GetNext(pos);
  268.         if (pSock == pSocket)
  269.         {
  270.             m_connectionList.RemoveAt(temp);
  271.             break;
  272.         }
  273.     }
  274.  
  275.     delete pSocket;
  276. }
  277.  
  278. void CServerDoc::Message(LPCTSTR lpszMessage)
  279. {
  280.     if( m_viewList.GetCount() != 0 )
  281.         ((CServerView*)m_viewList.GetHead())->Message(lpszMessage);
  282. }
  283.  
  284. /////////////////////////////////////////////////////////////////////////////
  285. // CServerDoc Handlers
  286.  
  287. void CServerDoc::OnUpdateMessages(CCmdUI* pCmdUI) 
  288. {
  289.     pCmdUI->Enable(TRUE);
  290.  
  291.     CString strFmt;
  292.     if (strFmt.LoadString(IDS_MESSAGESFMT))
  293.     {
  294.         CString strTemp;
  295.         wsprintf(strTemp.GetBuffer(50),strFmt,m_msgList.GetCount());
  296.         strTemp.ReleaseBuffer();
  297.         pCmdUI->SetText(strTemp);
  298.     }
  299. }
  300.  
  301. void CServerDoc::OnUpdateConnections(CCmdUI* pCmdUI) 
  302. {
  303.     pCmdUI->Enable(TRUE);
  304.  
  305.     CString strFmt;
  306.     if (strFmt.LoadString(IDS_CONNECTIONSFMT))
  307.     {
  308.         CString strTemp;
  309.         wsprintf(strTemp.GetBuffer(50),strFmt,m_connectionList.GetCount());
  310.         strTemp.ReleaseBuffer();
  311.         pCmdUI->SetText(strTemp);
  312.     }
  313. }
  314.  
  315.